package cn.wolfcode.business.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import cn.wolfcode.business.utils.DateUtils;
import cn.wolfcode.business.utils.RegexUtils;
import cn.wolfcode.business.utils.VehiclePlateNoUtil;
import org.springframework.stereotype.Service;
import cn.wolfcode.business.mapper.BusStatementMapper;
import cn.wolfcode.business.domain.BusStatement;
import cn.wolfcode.business.service.IBusStatementService;

import javax.annotation.Resource;

/**
 * 结算单Service业务层处理
 *
 * @author wolfcode
 * @date 2025-04-05
 */
@Service
public class BusStatementServiceImpl implements IBusStatementService {
    @Resource
    private BusStatementMapper busStatementMapper;

    /**
     * 查询结算单
     *
     * @param id 结算单主键
     * @return 结算单
     */
    @Override
    public BusStatement selectBusStatementById(Long id) {
        return busStatementMapper.selectBusStatementById(id);
    }

    /**
     * 查询结算单列表
     *
     * @param busStatement 结算单
     * @return 结算单
     */
    @Override
    public List<BusStatement> selectBusStatementList(BusStatement busStatement) {
        String setDate = (String) busStatement.getParams().get("endTime");
        Date date = DateUtils.parseDate(setDate, "yyyy-MM-dd HH:mm:ss");
        Date endDateTime = DateUtils.getEndTime(date);
        String endTime = DateUtils.formatDate(endDateTime, "yyyy-MM-dd HH:mm:ss");
        busStatement.getParams().put("endTime", endTime);
        return busStatementMapper.selectBusStatementList(busStatement);
    }

    /**
     * 新增结算单
     *
     * @param busStatement 结算单
     * @return 结果
     */
    @Override
    public int insertBusStatement(BusStatement busStatement) {
        //参数合法性校验
        if (busStatement == null) {
            throw new RuntimeException("非法参数");
        }
        //前台验证的参数后台都要验证
        if (busStatement.getCustomerName() == null || busStatement.getCustomerPhone() == null || busStatement.getActualArrivalTime() == null || busStatement.getLicensePlate() == null || busStatement.getCarSeries() == null || busStatement.getServiceType() == null) {
            throw new RuntimeException("非法参数");
        }
        //手机号码验证
        if(!RegexUtils.isPhoneLegal(busStatement.getCustomerPhone())){
            throw new RuntimeException("手机号码格式不正确");
        }
        //车牌号验证
        VehiclePlateNoUtil.VehiclePlateNoEnum vehiclePlateNo = VehiclePlateNoUtil.getVehiclePlateNo(busStatement.getLicensePlate());
        if(vehiclePlateNo == null){
            throw new RuntimeException("车牌号码格式不正确");
        }
        //到店时间需要早于当前时间
        Date date = new Date();
        if(!busStatement.getActualArrivalTime().before(date)){
            throw new RuntimeException("到店时间异常");
        }
        //安全性考虑，将新插入的数据设置为消费中
        busStatement.setStatus(BusStatement.STATUS_CONSUME);

        busStatement.setCreateTime(new Date());
        return busStatementMapper.insertBusStatement(busStatement);
    }

    /**
     * 修改结算单
     *
     * @param busStatement 结算单
     * @return 结果
     */
    @Override
    public int updateBusStatement(BusStatement busStatement) {
        //参数合法性校验
        if (busStatement == null) {
            throw new RuntimeException("非法参数");
        }
        //前台验证的参数后台都要验证
        if (busStatement.getCustomerName() == null || busStatement.getCustomerPhone() == null || busStatement.getActualArrivalTime() == null || busStatement.getLicensePlate() == null || busStatement.getCarSeries() == null || busStatement.getServiceType() == null) {
            throw new RuntimeException("非法参数");
        }
        //手机号码验证
        if(!RegexUtils.isPhoneLegal(busStatement.getCustomerPhone())){
            throw new RuntimeException("手机号码格式不正确");
        }
        //车牌号验证
        VehiclePlateNoUtil.VehiclePlateNoEnum vehiclePlateNo = VehiclePlateNoUtil.getVehiclePlateNo(busStatement.getLicensePlate());
        if(vehiclePlateNo == null){
            throw new RuntimeException("车牌号码格式不正确");
        }
        //到店时间需要早于当前时间
        Date date = new Date();
        if(!busStatement.getActualArrivalTime().before(date)){
            throw new RuntimeException("到店时间异常");
        }
        //状态必须处于消费中才能修改
        BusStatement busStatement1 = this.selectBusStatementById(busStatement.getId());
        if(!BusStatement.STATUS_CONSUME.equals(busStatement1.getStatus())){
            throw new RuntimeException("状态必须为消费中才能修改");
        }
        //安全设置
        busStatement.setStatus(busStatement1.getStatus());
        return busStatementMapper.updateBusStatement(busStatement);
    }

    /**
     * 批量删除结算单
     *
     * @param ids 需要删除的结算单主键
     * @return 结果
     */
    @Override
    public int deleteBusStatementByIds(Long[] ids) {
        if(ids == null || ids.length <= 0){
            throw new RuntimeException("非法参数");
        }
        Long id = ids[0];
        BusStatement busStatement = this.selectBusStatementById(id);
        if(busStatement == null){
            throw new RuntimeException("非法参数");
        }
        if(!BusStatement.STATUS_CONSUME.equals(busStatement.getStatus())){
            throw new RuntimeException("只有状态为消费中的记录才可以删除");
        }
        return busStatementMapper.deleteBusStatementByIds(ids);
    }

    /**
     * 删除结算单信息
     *
     * @param id 结算单主键
     * @return 结果
     */
    @Override
    public int deleteBusStatementById(Long id) {
        return busStatementMapper.deleteBusStatementById(id);
    }

    /*根据结算单id修改总金额、总数量、折扣价*/
    @Override
    public int updateAmount(Long statementId, BigDecimal totalAmount, BigDecimal totalQuantity, BigDecimal discountAmount) {
        return busStatementMapper.updateAmount(statementId,totalAmount,totalQuantity,discountAmount);
    }

    @Override
    public int updatePayStatusByStatementId(Long statementId, Long payId, Date date, Integer statusPaid) {
        return busStatementMapper.updatePayStatusByStatementId(statementId,payId,date,BusStatement.STATUS_PAID);
    }

    /*根据预约单id查询结算单对象*/
    @Override
    public BusStatement selectByAppointmentId(Long appointmentId) {
        return busStatementMapper.selectBusStatementByAppointmentId(appointmentId);
    }

}
